﻿using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using webapp4U.BOL;

namespace webapp4U.UI
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Title.TitleName = ResourceManager.GetString("Product_ThongtinKhachHang");

            if (Globals.CurrentUserName == "")
                Response.Redirect(Globals.ApplicationPath + "Logon.aspx");
            else
                this.LoadProfile();
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            if (ShoppingCart.GetSubTotalShoppingCart() > 0)
            {
                Orders obj = new Orders();
                obj.AddedDate = DateTime.Now;
                obj.AddedBy = Globals.CurrentUserName;
                obj.StatusID = 1;
                obj.ShippingMethod = dlPaymentMethod.SelectedItem.Text;
                obj.SubTotal = ShoppingCart.GetSubTotalShoppingCart();
                obj.Shipping = 0;
                obj.TaxCode = "";
                obj.ShippingName = txtFullName.Text;
                obj.ShippingAddress = txtAddress.Text;
                obj.ShippingPostalCode = "";
                obj.ShippingCity = ddlJobLocation.SelectedItem.Text;
                obj.ShippingCountry = ddlCountry.SelectedItem.Text;
                obj.CustomerEmail = txtEmail.Text;
                obj.CustomerPhone = txtPhone.Text;
                obj.CustomerFax = txtFax.Text;
                obj.ShippedDate = DateTime.Now;
                obj.TransactionID = "";
                obj.TrackingID = "";
                int OrderID = Orders.Insert(obj);

                // OrderItems
                InsertOrderItems(OrderID);

                this.Send_Mail();

                Response.Redirect(Globals.ApplicationPath + "Products/ThongtinThanhtoan.aspx");
            }
        }

        private void InsertOrderItems(int OrderID)
        { 
            //load ShoppingCart
            List<ShoppingCart> list = ShoppingCart.GetShoppingCartByUserID();
            foreach (ShoppingCart f in list)
            {
                OrderItems obj = new OrderItems();
                obj.OrderID = OrderID;
                obj.AddedBy = Globals.CurrentUserName;
                obj.AddedDate = DateTime.Now;
                obj.ProductID = f.ProductID;
                obj.Title = f.Title;
                obj.SKU = f.SKU;
                obj.Quantity = f.Quantity;
                obj.UnitPrice = f.UnitPrice;
                OrderItems.Insert(obj);
            }
        }


        protected void LoadProfile()
        {
            webapp4U.BOL.User obj = webapp4U.BOL.User.GetUserByID(Globals.CurrentUserID);
            txtEmail.Text = obj.Email;
            txtAddress.Text = obj.Address;
            txtPhone.Text = obj.Phone;
            txtMobile.Text = obj.Mobile;
            txtFax.Text = obj.Fax;
            ddlCountry.SelectedValue = obj.CountryID.ToString().Trim();
            ddlJobLocation.SelectedValue = obj.ProvinceID.ToString().Trim();

            //jobSeeker
            txtFullName.Text = obj.FirstName;
        }


        private void Send_Mail()
        {
            try
            {
                // Lay thong tin lien he
                ContactSetting obj = ContactSetting.GetContactSettingByID(1);
                string EmailTo = obj.Email;

                // goi email cho admin website
                SendMail sendmail = new SendMail();
                sendmail.MailServer = ConfigurationManager.AppSettings["SmtpServer"].ToString();
                sendmail.MailFrom = ConfigurationManager.AppSettings["EmailFrom"].ToString();
                sendmail.MailTo = EmailTo;
                sendmail.Subject = "Thông báo có đơn đặc hàng";

                // noi dung can goi
                StringBuilder strBody = new StringBuilder(string.Empty);
                strBody.Append(ResourceManager.GetString("Thông tin đơn hàng"));

                strBody.AppendLine("<br>");
                strBody.AppendLine("<hr>");

                strBody.Append("Ngày: " + Formatter.FormatDate(DateTime.Now));
                strBody.AppendLine("<br>");

                strBody.Append("Họ tên: " + txtFullName.Text);
                strBody.AppendLine("<br>");

                strBody.Append("Địa chỉ: " + txtAddress.Text);
                strBody.AppendLine("<br>");

                strBody.Append("Email: " + txtEmail.Text);
                strBody.AppendLine("<br>");

                strBody.Append(ResourceManager.GetString("Phone") + ": " + txtPhone.Text);
                strBody.AppendLine("<br>");

                strBody.Append(ResourceManager.GetString("Mobile") + ": " + txtMobile.Text);
                strBody.AppendLine("<br>");

                strBody.Append(ResourceManager.GetString("Fax") + ": " + txtFax.Text);
                strBody.AppendLine("<br>");

                strBody.Append(ResourceManager.GetString("Thương thức thanh toán") + ": " + dlPaymentMethod.SelectedItem.Text);
                strBody.AppendLine("<br>");

                strBody.AppendLine("<br>");
                strBody.AppendLine("<hr>");


                sendmail.Body = strBody.ToString();
                webapp4U.SendMail.Send(sendmail);

                lblMsg.Text = strBody.ToString();

            }
            catch (Exception ex)
            {
                Common.ErrorMessage("Xay ra loi khi goi mail thanh toan: " + ex.ToString());
            }

        }

    }
}